package com.sunyard.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * @ Program       :  com.ljnt.blog.utils.TokenUtil
 * @ Description   :  token工具类（生成、验证）
 * @ Author        :  lj
 * @ CreateDate    :  2020-1-31 22:15
 */
@Slf4j
public class TokenUtil {

    public static final long EXPIRE_TIME = 20 * 60 * 1000;//token在前端到期时间10分钟，毫秒为单位
    public static final long REDIS_EXPIRE_TIME = 30 * 60L;//token在redis到期时间30分钟，秒为单位
    public static final long REFRESH_EXPIRE_TIME = 30 * 60L;//RefreshToken到期时间为30分钟，秒为单位
    public static final long RANDOM_TIME = 5 * 60 * 1000;//验证码到期时间为50分钟，秒为单位
    private static final String TOKEN_SECRET = "ljdyaishijin**3nkjnj??";  //密钥盐

    /**
     * @param : [user]
     * @return : java.lang.String
     * @throws :
     * @Description ：生成token（管理端）
     * @author : lj
     * @date : 2020-1-31 22:49
     */
    public static String sign(String opId, String opName, String orgId, String roleId, String orgLevel, String auditType, Long currentTime,long time) {

        String token = null;
        Date expireAt = new Date(currentTime + time);
        token = JWT.create()
                //发行人
                .withIssuer("auth0")
                //存放数据
                .withClaim("opId", opId)
                .withClaim("opName", opName)
                .withClaim("orgId", orgId)
                .withClaim("currentTime", currentTime)
                .withClaim("roleId", roleId)
                .withClaim("orgLevel", orgLevel)
                .withClaim("auditType", auditType)
                //过期时间
                .withExpiresAt(expireAt)
                .sign(Algorithm.HMAC256(TOKEN_SECRET));
        return token;
    }

    /**
     * @param : [user]
     * @return : java.lang.String
     * @throws :
     * @Description ：生成token（商服）
     * @author : lj
     * @date : 2020-1-31 22:49
     */
    public static String signMerchant(String account, Long currentTime,long time) {

        String token = null;
        Date expireAt = new Date(currentTime + time);
        token = JWT.create()
                //发行人
                .withIssuer("auth0")
                //存放数据
                .withClaim("account", account)
                //存放数据
                .withClaim("currentTime", currentTime)
                //过期时间
                .withExpiresAt(expireAt)
                .sign(Algorithm.HMAC256(TOKEN_SECRET));
        return token;
    }

    /**
     * @param : [user]
     * @return : java.lang.String
     * @throws :
     * @Description ：生成验证码token（商服）
     * @author : ljf
     */
    public static String signRandom(String account, Long currentTime) {

        String token = null;
        Date expireAt = new Date(currentTime + RANDOM_TIME);
        token = JWT.create()
                //发行人
                .withIssuer("auth0")
                //存放数据
                .withClaim("account", account)
                //存放数据
                .withClaim("currentTime", currentTime)
                //过期时间
                .withExpiresAt(expireAt)
                .sign(Algorithm.HMAC256(TOKEN_SECRET));
        return token;
    }

    /**
     * @param : [token]
     * @return : java.lang.Boolean
     * @throws :
     * @Description ：token验证
     * @author : ljf
     */
    public static Boolean verify(String token) throws Exception {

        JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(TOKEN_SECRET)).withIssuer("auth0").build();//创建token验证器
        DecodedJWT decodedJWT = jwtVerifier.verify(token);
        log.info("token认证通过，过期时间为：" + DateUtil.getDefaultDateStr(decodedJWT.getExpiresAt()));
        return true;
    }


    public static String getOpId(String token) {
        DecodedJWT decodedJWT = JWT.decode(token);
        return decodedJWT.getClaim("opId").asString();
    }

    public static String getOpName(String token) {
        DecodedJWT decodedJWT = JWT.decode(token);
        return decodedJWT.getClaim("opName").asString();
    }

    public static String getOrgId(String token) {
        DecodedJWT decodedJWT = JWT.decode(token);
        return decodedJWT.getClaim("orgId").asString();
    }

    public static String getAccount(String token) {
        DecodedJWT decodedJWT = JWT.decode(token);
        return decodedJWT.getClaim("account").asString();
    }

    public static Long getCurrentTime(String token) {
        DecodedJWT decodedJWT = JWT.decode(token);
        return decodedJWT.getClaim("currentTime").asLong();
    }

    public static String getRoleId(String token) {
        DecodedJWT decodedJWT = JWT.decode(token);
        return decodedJWT.getClaim("roleId").asString();
    }

    public static String getOrgLevel(String token) {
        DecodedJWT decodedJWT = JWT.decode(token);
        return decodedJWT.getClaim("orgLevel").asString();
    }

    public static String getAuditType(String token) {
        DecodedJWT decodedJWT = JWT.decode(token);
        return decodedJWT.getClaim("auditType").asString();
    }
}
